//
//  MYGSlideTransition.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/4.
//

import UIKit

//enum MYGSlideAnimationDriection {
//    case top
//    case bottom
//}

class MYGSlideTransition: NSObject, UIViewControllerAnimatedTransitioning {
    private var transitionContext: UIViewControllerContextTransitioning?
    
    private var isPresent: Bool = true
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        0.1
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        if isPresent {
            presentAnimation(transitionContext)
        } else {
            dissmissAnimation(transitionContext)
        }
    }
    
    private func presentAnimation(_ transitionContext: UIViewControllerContextTransitioning) {
        self.transitionContext = transitionContext
        guard let toVc = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? MYGSliderController else {
            return
        }

        let containerView = transitionContext.containerView
        let toView = toVc.mainView
        containerView.addSubview(toView)
        containerView.addSubview(maskView)
        
        toView.snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.left.right.bottom.equalToSuperview()
        }
        
        maskView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalTo(toView.snp.top)
        }
        
        toView.layoutIfNeeded()
        
        let duration = transitionDuration(using: transitionContext)
        
        let animation = CABasicAnimation(keyPath: "position")
        animation.fromValue = CGPoint(x: screenWidth/2, y: screenHeight+(toView.bounds.size.height/2))
        animation.toValue = CGPoint(x: screenWidth/2, y: screenHeight-(toView.bounds.size.height)/2)
        animation.isRemovedOnCompletion = false
        animation.fillMode = .forwards
        animation.duration = duration
        animation.delegate = self
        toView.layer.add(animation, forKey: "begin")
    }
    
    private func dissmissAnimation(_ transitionContext: UIViewControllerContextTransitioning) {
        self.transitionContext = transitionContext
        guard let fromVc = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as? MYGSliderController else {
            return
        }

        let containerView = transitionContext.containerView
        maskView.isHidden = true
        
        let fromView = fromVc.mainView
        containerView.addSubview(fromView)
    
        fromView.snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.left.right.bottom.equalToSuperview()
        }
        
        fromView.layoutIfNeeded()
        
        let duration = transitionDuration(using: transitionContext)
        
        let animation = CABasicAnimation(keyPath: "position")
        animation.toValue = CGPoint(x: screenWidth/2, y: screenHeight+(fromView.bounds.size.height/2))
        animation.fromValue = CGPoint(x: screenWidth/2, y: screenHeight-(fromView.bounds.size.height)/2)
        animation.isRemovedOnCompletion = false
        animation.fillMode = .forwards
        animation.duration = duration
        animation.delegate = self
        fromView.layer.add(animation, forKey: "end")
    }
    
    @objc
    private func dissmissView() {
        UIViewController.currentViewController?.dismiss(animated: true)
    }
    
    // MARK: - lazy load
    private lazy var maskView: UIButton = {
        let view = UIButton()
        view.backgroundColor = .black.withAlphaComponent(0.3)
        view.isHidden = true
        
        view.addTarget(self, action: #selector(dissmissView), for: .touchUpInside)
        return view
    }()
}

extension MYGSlideTransition: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}

extension MYGSlideTransition: CAAnimationDelegate {
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        maskView.isHidden = false
        transitionContext?.completeTransition(true)
    }
}
